package 线程;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestExecutors线程池 {
    public static void main(String[] args) {
        Ticket4 t = new Ticket4();
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(t);
        Thread t3 = new Thread(t);
        Thread t4 = new Thread(t);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        //
        ExecutorService pool = Executors.newFixedThreadPool(5);//5个线程数
        for (int i = 0; i < 5; i++) {  //循环五次
            pool.execute(t);//传参t,是售票对象;执行五次
        }
    }
}
class Ticket4 implements Runnable{
//
  static   int tickets =100;
    @Override
    public void run() {
        //不需要同步锁，或者读写锁
        while(true){//循环卖票
            if(tickets>0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "=" + tickets--);//Thread-0=92
            }if(tickets>=0) break;
        }

    }
}